﻿'Imports CoreFX
Namespace Client
    <Serializable()> _
    Public Class IRCClient
        Inherits Connections.IRCConnection
        Dim A As String
        Dim FindVar As Client.Channel.ChannelItem
        Event ChangedNick(ByVal PrevNick As String, ByVal NewNick As String)
        Event UserJoined(ByVal User As String, ByVal ChannelName As String)
        Event KickedFromChan(ByVal E As Connections.KickedEventArgs)
        Event NewQuery(ByVal User As String, ByVal Message As String, ByVal IsFirstMessage As Boolean)
        Event NewMessageRcvd(ByVal User As String, ByVal Message As String, ByVal ChannelName As String)
        Event JoinNewChannel(ByVal ChannelName As String, ByVal ChannelObj As Client.Channel.IRCChannel)
        Event InviteToChan(ByVal ChannelName As String)
        Event TopicChange(ByVal Topic As String, ByVal ChannelName As String)
        Event RecvNickList(ByVal Channel As String, ByVal E As Connections.NickListRecvArgs)
        Event ChannelModeChanged(ByVal E As Connections.ChannelModeArgs)
        Event UserModeChanged(ByVal E As Connections.UserModeArgs)
        Event BanListRecvd(ByVal E As Connections.BanListArgs)
        Event IRCNoticedRecv(ByVal E As Connections.GenericEventArgs)
#Region " Event Abstraction "
        Public Sub New()
            AddHandler BanListReceived, AddressOf BlistRecvdHandled
            AddHandler MyBase.UserJoin, AddressOf UserJoinedHandled
            AddHandler MyBase.TopicChanged, AddressOf ChangeTopic
            AddHandler MyBase.NewMessage, AddressOf NewMessageHandled
            AddHandler MyBase.Query, AddressOf HandleQuery
            AddHandler MyBase.Kicked, AddressOf KickedHandled
            AddHandler MyBase.InviteUser, AddressOf InviteUserToChannel
            AddHandler MyBase.NickListRecv, AddressOf NickListRecvHandled
            AddHandler MyBase.ChannelModeChange, AddressOf CMCHandled
            AddHandler MyBase.UserQuit, AddressOf UserQuitHandled
            AddHandler MyBase.UserPart, AddressOf UserPartHandled
            AddHandler MyBase.UserModeChange, AddressOf UserModeChangedHandled
            AddHandler MyBase.NickChanged, AddressOf NickChangedHandled
            AddHandler MyBase.IRCNotice, AddressOf IRCNoticeHandled
        End Sub
        Private Sub IRCNoticeHandled(ByVal E As Connections.GenericEventArgs)
            If Not IsNothing(SharedVars.GlobalChannelWindow) Then SharedVars.GlobalChannelWindow.HandleNotice(E)
        End Sub
        Private Sub BlistRecvdHandled(ByVal E As Connections.BanListArgs)
            RaiseEvent BanListRecvd(E)
        End Sub
        Private Sub NickChangedHandled(ByVal PrevNick As String, ByVal NewNick As String)
            FindVar = SharedVars.Channels.Item(0)
            If Not IsNothing(FindVar.ChanObj) Then
                If Not IsNothing(FindVar.ChanObj.ChannelWindow) Then
                    FindVar.ChanObj.ChannelWindow.ChangeNick(PrevNick, NewNick)
                End If
            End If
        End Sub
        Private Sub UserModeChangedHandled(ByVal E As Connections.UserModeArgs)
            RaiseEvent UserModeChanged(E)
        End Sub
        Private Sub UserPartHandled(ByVal User As String, ByVal Channel As String)
            FindVar = SharedVars.Channels.Find(Channel)
            If Not IsNothing(FindVar.ChanObj) Then
                If Not IsNothing(FindVar.ChanObj.ChannelWindow) Then
                    FindVar.ChanObj.ChannelWindow.PartFromChannel(User, Channel)
                End If
            End If
        End Sub
        Private Sub UserQuitHandled(ByVal User As String, ByVal Reason As String)
            FindVar = SharedVars.Channels.Item(0)
            If Not IsNothing(FindVar.ChanObj) Then
                If Not IsNothing(FindVar.ChanObj.ChannelWindow) Then
                    FindVar.ChanObj.ChannelWindow.UserQuit(User, Reason)
                End If
            End If
        End Sub
        Private Sub CMCHandled(ByVal E As Connections.ChannelModeArgs)
            FindVar = SharedVars.Channels.Find(E.Channel)
            If Not IsNothing(FindVar.ChanObj) Then
                If Not IsNothing(FindVar.ChanObj.ChannelWindow) Then
                    FindVar.ChanObj.ChannelWindow.ChannelModeChanged(E)
                End If
            End If
        End Sub
        Private Sub NickListRecvHandled(ByVal Channel As String, ByVal e As Connections.NickListRecvArgs)
            '            [Global].Services.Get(Of Interfaces.ILogging).AddItem("NickListRecvHandled: " & e.ListNicks.Length, Logging.MessageType.NetworkLayer)
            If Not IsNothing(SharedVars.GlobalChannelWindow) Then SharedVars.GlobalChannelWindow.LoadNickList(Channel, e)
        End Sub
        Private Sub HandleQuery(ByVal User As String, ByVal Message As String, ByVal IsFirstMessage As Boolean)
            If Not IsNothing(SharedVars.GlobalChannelWindow) Then SharedVars.GlobalChannelWindow.NewQuery(User, Message, IsFirstMessage)
        End Sub
        Private Sub InviteUserToChannel(ByVal ChannelName As String)
            RaiseEvent InviteToChan(ChannelName)
        End Sub
        Private Sub ChangeTopic(ByVal Topic As String, ByVal ChannelName As String)
            If Not IsNothing(SharedVars.GlobalChannelWindow) Then SharedVars.GlobalChannelWindow.ChangeTopic(Topic, ChannelName)
        End Sub
        Public Sub NewMessageHandled(ByVal User As String, ByVal Message As String, ByVal ChannelName As String)
            If Not IsNothing(SharedVars.GlobalChannelWindow) Then SharedVars.GlobalChannelWindow.NewMessage(User, Message, ChannelName)
        End Sub
        Private Sub UserJoinedHandled(ByVal User As String, ByVal ChannelName As String)
            If Not IsNothing(SharedVars.GlobalChannelWindow) Then SharedVars.GlobalChannelWindow.UserJoined(User, ChannelName)
        End Sub
        Private Sub KickedHandled(ByVal E As Connections.KickedEventArgs)
            If Not IsNothing(SharedVars.GlobalChannelWindow) Then SharedVars.GlobalChannelWindow.Kicked(E)
        End Sub
#End Region
#Region " Command Abstraction"
        Public Sub Login(ByVal Username As String, ByVal Password As String, ByVal Nick As String)
            Try
                WriteToStream("PASS " & Password)
                WriteToStream("NICK " & Nick)
                SharedVars.Nick = Nick
                Dim logstr As String = "USER " & Username & " " & "0" & " * :" & "rs"
                WriteToStream(logstr)
                '  [Global].Services.Get(Of Interfaces.ILogging).AddItem("Login: " & logstr, Logging.MessageType.General)
            Catch ex As Exception
                '[Global].Services.Get(Of Interfaces.ILogging).AddItem("Login: " & ex.ToString, Logging.MessageType.General)
            End Try
        End Sub
        Public Function ReturnMyUserName() As String
            A = GetSetting("MChat", "Settings", "DefaultChannel")
            A = GetSetting("MChat", A, "User")
            Return A
        End Function
        Public Sub RawCommand(ByVal Command As String)
            WriteToStream(Command & vbNewLine)
        End Sub
        Public Sub ConnectToServer(ByVal Address As String, ByVal Port As String)
            Connect(Port, Address)
        End Sub
        Public Sub ChangeNickname(ByVal Nick As String)
            WriteToStream("NICK :" & Nick & vbNewLine)
        End Sub
        Public Overridable Sub WriteRawNSCommand(ByVal CommandName As String, ByVal Params As String())
            WriteToStream("PRIVMSG NICKSERV " & CommandName & " " & Params(0) & " " & Params(1))
        End Sub
        Public Sub Kick(ByVal ChannelName As String, ByVal Nick As String, ByVal Reason As String)
            WriteToStream("KICK " & ChannelName & " " & Nick & " :" & Reason)
        End Sub
        Public Sub IDNick(ByVal Password As String)
            WriteToStream("PRIVMSG NICKSERV IDENTIFY " & Password)
        End Sub
        Public Sub Disconnect(Optional ByVal Message As String = "Using MChat --- http://codeplex.com/mchat")
            WriteToStream("QUIT :" & Message & vbNewLine)
        End Sub
        Public Sub SendMessage(ByVal ChannelName As String, ByVal Message As String)
            WriteToStream("PRIVMSG " & ChannelName & " :" & Message)
        End Sub
        Public Sub JoinChannel(ByVal ChannelName As String)
            WriteToStream("JOIN " & ChannelName & vbNewLine)
        End Sub
        Public Sub PartChannel(ByVal ChannelName As String)
            Try
                WriteToStream("PART " & ChannelName & vbNewLine)
                FindVar = SharedVars.Channels.Find(ChannelName)
                FindVar.ChanObj.ChannelWindow.PartFromChannel("", ChannelName)
                If FindVar.ChanObj IsNot Nothing Then SharedVars.Channels.Remove(FindVar)
            Catch ex As Exception
            End Try
        End Sub
        Public Sub SetTopic(ByVal ChannelName As String, ByVal Topic As String)
            WriteToStream("TOPIC " & ChannelName & " :" & Topic)
        End Sub
        Public Sub StartQuery(ByVal User As String)
            RaiseEvent NewQuery(User, "", True)
        End Sub
#End Region
    End Class
End Namespace